package org.chengn.level_middle.context2304;

public class Context2304 {
    public int minPathCost(int[][] grid, int[][] moveCost) {

        int m = grid.length, n = grid[0].length;

        int[][] g = new int[grid.length][grid[0].length];
        // for (int i = 0; i < n; ++i) g[0][i] = grid[0][i];
        for (int i = 1; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                int resLen = Integer.MAX_VALUE;
                for (int k = 0; k < n; ++k) {
                    resLen = Math.min(resLen, grid[i - 1][k] + moveCost[grid[i - 1][k]][j] + g[i - 1][k]);
                }
                g[i][j] = resLen;
            }
        }
        int res = Integer.MAX_VALUE;
        for (int i = 0; i < n; ++i) {
            res = Math.min(res, g[m - 1][i] + grid[m - 1][i]);
        }
        return res;
    }

    private int min(int a, int b, int c) {
        return Math.min(a, Math.min(b, c));
    }
}
